package xiaolong.lian.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xiaolong.lian.entity.Doc;
import xiaolong.lian.exception.CustomException;
import xiaolong.lian.service.DocService;
import xiaolong.lian.util.Args;
import xiaolong.lian.util.Cookies;

import java.util.Arrays;

@Controller
@RequestMapping("/document")
public class DocController {
    @Autowired
    private DocService docService;

    @RequestMapping(value = "/list" , method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getDocList(){
        String tel = Cookies.get("tel");
        return docService.getDocList(tel).toString();
    }

    @RequestMapping(value = "/create" , method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String create(String name) throws CustomException {
        new Args().ensureNotEmpty("文档名", name);
        if(name.length() > 60)
            name = name.substring(0, 60);
        Doc doc = docService.createDoc(name, Cookies.get("tel"));
        return doc.toString();
    }

    @RequestMapping(value = "/delete" , method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delete(String id) throws CustomException {
        new Args().ensureNotEmpty("文档id", id);
        docService.deleteDoc(id, Cookies.get("tel"));
        return "{\"msg\" : \"删除文档成功\"}";
    }

    @RequestMapping(value = "/rename" , method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String rename(String id, String name) throws CustomException {
        new Args().ensureNotEmpty("文档id", id).ensureNotEmpty("文档新名称", name);
        docService.renameDoc(id, name, Cookies.get("tel"));
        return "{\"msg\" : \"文档重命名成功\"}";
    }

    /**
     * 初始的本地文档可直接同步成在线文档
     * 保存其页面的所有数据
     * @param name
     * @param pageData
     * @return
     */
    @RequestMapping(value = "/sync" , method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String syncLocalDoc(String name, String pageData) throws CustomException {
        new Args().ensureNotEmpty("文档名", name).ensureNotEmpty("页面数据", pageData);
        if(name.length() > 60)
            name = name.substring(0, 60);
        JSONArray array = JSON.parseArray(pageData);
        if(array.size() == 0)
            throw new CustomException("没有页面数据");
        LoggerFactory.getLogger(getClass()).info("size -> " + array.size());
        return docService.syncLocalDoc(name, array.toJavaList(String.class), Cookies.get("tel")).toString();
    }
}
